package notes.java.zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.List;

import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.IOUtils;

import com.google.common.collect.Lists;

/**
 * 
 */
public class ZipUtil {

	/**
	 * 压缩到zip中
	 * @param compressPath zip文件全路径 
	 * @param needCompressPaths 被压缩的文件全路径
	 */
	public static final void zip(String compressPath, String[] needCompressPaths) {
		File compressFile = new File(compressPath);

		List<File> files = Lists.newArrayList();
		for (String needCompressPath : needCompressPaths) {
			File needCompressFile = new File(needCompressPath);
			if (!needCompressFile.exists()) {
				continue;
			}
			files.add(needCompressFile);
		}
		try (ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(compressFile)) {
			zaos.setUseZip64(Zip64Mode.AsNeeded);
			zaos.setEncoding("GBK");
			for (File file : files) {
				addFilesToCompression(zaos, file, "");
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void addFilesToCompression(ZipArchiveOutputStream zaos, File file, String dir) throws IOException {

		ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, dir + file.getName());
		zaos.putArchiveEntry(zipArchiveEntry);

		if (file.isFile()) {
			try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
				IOUtils.copy(bis, zaos);
				zaos.closeArchiveEntry();
			}
		} else if (file.isDirectory()) {
			zaos.closeArchiveEntry();

			for (File childFile : file.listFiles()) {
				addFilesToCompression(zaos, childFile, dir + file.getName() + File.separator);
			}
		}
	}

	/**
	 * 解压zip包
	 * @param path zip包路径 
	 * @param descPath 解压目的地
	 * @param override 是否覆盖
	 */
	public static void unzip(String path, String descPath, boolean override) {
		File uncompressFile = new File(path);
		File descPathFile = new File(descPath);
		unzipFolder(uncompressFile, descPathFile, override);

	}

	private static void unzipFolder(File uncompressFile, File descPathFile, boolean override) {

		try (ZipFile zipFile = new ZipFile(uncompressFile, "GBK")) {

			Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
			while (entries.hasMoreElements()) {
				ZipArchiveEntry zipEntry = entries.nextElement();
				String name = zipEntry.getName();
				name = name.replace("\\", "/");

				File currentFile = new File(descPathFile, name);

				// 非覆盖 跳过
				if (currentFile.isFile() && currentFile.exists() && !override) {
					continue;
				}

				if (name.endsWith("/")) {
					currentFile.mkdirs();
					continue;
				} else {
					currentFile.getParentFile().mkdirs();
				}

				try (FileOutputStream fos = new FileOutputStream(currentFile)) {
					InputStream is = zipFile.getInputStream(zipEntry);
					IOUtils.copy(is, fos);
				}
			}
		} catch (IOException e) {
			throw new RuntimeException("解压缩失败", e);
		}

	}

	public static void main(String[] args) {
		// 文件压缩到zip包中
		String[] needCompress = new String[] { "d:/turboc/turboc2.0.zip", "d:/turboc/Turbo_C_2.0_使用.doc", "d:/turboc/tcpp3full.zip" };
		zip("D:\\turboc\\testZip.zip", needCompress);

		// 解压
		unzip("D:\\turboc\\turboc2.0.zip", "D:\\turboc\\aaaa", true);
	}
}